package com.example.weight.greenSeasons.SolidLiquid;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class WeighingProductionSiteRecordForm_stool1_1 extends AppCompatActivity {

    private TitleBar titleBar;
    //时间
    private EditText editTextDate,editTextStartTime,editTextCloseTime;
    //判断属性
    private EditText editTextChengLiang,editTextJianKong,editTextShuJu,editTextYuanCheng;
    //来源
    private EditText hd_checi,hd_zhongliang,sq_checi,sq_zhongliang,bs_checi,bs_zhongliang;
    private EditText tf_checi,tf_zhongliang,jz_checi,jz_zhongliang,gy_checi,gy_zhongliang;
    //操作人和备注
    private EditText operator,additon;
    private String factory;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weighing_production_site_record_form_stool1_1);

        Intent previousIntent = getIntent();
        factory = previousIntent.getStringExtra("factory");
        //时间选择
        editTextDate=findViewById(R.id.weight_fenbian_date);
        editTextStartTime=findViewById(R.id.weight_fenbian_startTime);
        editTextCloseTime=findViewById(R.id.weight_fenbian_closeTime);

        //判断属性
        editTextChengLiang=findViewById(R.id.weight_chengliang);
        editTextJianKong=findViewById(R.id.weight_jiankong);
        editTextShuJu=findViewById(R.id.weight_shuju);
        editTextYuanCheng=findViewById(R.id.weight_yuancheng);

        //来源
        //海淀
        hd_checi=findViewById(R.id.weight_haiding_checi);
        hd_zhongliang=findViewById(R.id.weight_haiding_zhongliang);
        //四清分公司
        sq_checi=findViewById(R.id.weight_siqing_checi);
        sq_zhongliang=findViewById(R.id.weight_siqing_zhongliang);
        //宝山
        bs_checi=findViewById(R.id.weight_baoshan_checi);
        bs_zhongliang=findViewById(R.id.weight_baoshan_zhongliang);
        //田丰
        tf_checi=findViewById(R.id.weight_tianfeng_checi);
        tf_zhongliang=findViewById(R.id.weight_tianfeng_zhongliang);
        //进站粪便合计
        jz_checi=findViewById(R.id.weight_jinzhan_checi);
        jz_zhongliang=findViewById(R.id.weight_jinzhan_zhongliang);
        //固液分离残渣
        gy_checi=findViewById(R.id.weight_guye_checi);
        gy_zhongliang=findViewById(R.id.weight_guye_zhongliang);

        //操作人与备注
        operator=findViewById(R.id.weight_fenbian_operator);
        additon=findViewById(R.id.weight_fenbian_additon);

        //页面返回按钮
        titleBar=findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                WeighingProductionSiteRecordForm_stool1_1.this.finish();
            }
            @Override
            public void onTitleClick(View v) {
            }
            @Override
            public void onRightClick(View v) {
            }
        });

        //设置日期，年月日
        editTextDate.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int YEAR = calendar.get(Calendar.YEAR);
                int MONTH = calendar.get(Calendar.MONTH);
                int DAY = calendar.get(Calendar.DAY_OF_MONTH);
//                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
//                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                DatePickerDialog datePickerDialog = new DatePickerDialog(WeighingProductionSiteRecordForm_stool1_1.this, 2, (view1, year, month, day) -> {
//                    // 更新EditText控件日期 小于10加0
//                    TimePickerDialog timePickerDialog = new TimePickerDialog(WeighingProductionSiteRecordForm_stool1_1.this, 2, (view11, hourOfDay, minute) -> {
//                        //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
//                        editTextDate.setText(new StringBuilder()
//                                .append(year)
//                                .append("/")
//                                .append((month + 1) < 10 ? "0"
//                                        + (month + 1) : (month + 1))
//                                .append("/")
//                                .append((day < 10) ? "0" + day : day)
//                                .append(" ")
//                                .append(String.format("%02d",hourOfDay))
//                                .append(":")
//                                .append(String.format("%02d",minute)));
//                    }, HOUR, MINUTE, true);
//                    timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
//                        if (which == DialogInterface.BUTTON_NEGATIVE) {
//                            //取消操作
//                            editTextDate.clearFocus();
//                        }
//                    });
//                    timePickerDialog.show();

                   //更新EditText控件日期 小于10加0
                    editTextDate.setText(new StringBuilder()
                            .append(year)
                            .append("/")
                            .append((month + 1) < 10 ? "0"
                                    +month : (month + 1))
                            .append("/")
                            .append((day < 10) ? "0" + day : day));
                }, YEAR, MONTH, DAY);
                datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editTextDate.clearFocus();
                    }
                });
                datePickerDialog.show();
            }
        });

        //设置开机时间
        editTextStartTime.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                TimePickerDialog timePickerDialog = new TimePickerDialog(WeighingProductionSiteRecordForm_stool1_1.this, 2, (view11, hourOfDay, minute) -> {
                    //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                    editTextStartTime.setText(new StringBuilder()
                            .append(String.format("%02d",hourOfDay))
                            .append(":")
                            .append(String.format("%02d",minute)));
                }, HOUR, MINUTE, true);
                timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editTextStartTime.clearFocus();
                    }
                });
                timePickerDialog.show();
            }
        });
        //设置关机时间
        editTextCloseTime.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                Calendar calendar=Calendar.getInstance();
                int HOUR = calendar.get(Calendar.HOUR_OF_DAY);
                int MINUTE = calendar.get(Calendar.MINUTE);
                @SuppressLint({"DefaultLocale", "ResourceType"})
                TimePickerDialog timePickerDialog = new TimePickerDialog(WeighingProductionSiteRecordForm_stool1_1.this, 2, (view11, hourOfDay, minute) -> {
                    //  这个方法是得到选择后的 小时、分钟，分别对应着三个参数 —   hourOfDay、minute
                    editTextCloseTime.setText(new StringBuilder()
                            .append(String.format("%02d",hourOfDay))
                            .append(":")
                            .append(String.format("%02d",minute)));
                }, HOUR, MINUTE, true);
                timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                        //取消操作
                        editTextCloseTime.clearFocus();
                    }
                });
                timePickerDialog.show();
            }
        });

    }

    public void save(View view) {
        new AlertDialog.Builder(this)
                .setTitle("确定提交吗？")
                .setCancelable(false)
                .setNegativeButton("取消", ((dialog, which) ->
                        Toast.makeText(this, "取消提交", Toast.LENGTH_SHORT).show()))
                .setPositiveButton("确定", (dialog, which) -> {
                    // 提交表单
                    RequestQueue queue = Volley.newRequestQueue(this);
                    // 填写后半部分信息
                    if (editTextDate.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写日期", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextStartTime.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写开机时间", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextCloseTime.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写关机时间", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextChengLiang.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写称量软件情况", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextJianKong.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写监控图像情况", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextShuJu.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写数据同步情况", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (editTextYuanCheng.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写远程连接情况", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (hd_zhongliang.getText().toString().equals("")||hd_checi.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写海淀记录", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (sq_zhongliang.getText().toString().equals("")||sq_checi.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写四清分公司记录", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (bs_zhongliang.getText().toString().equals("")||bs_checi.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写宝山记录", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (tf_checi.getText().toString().equals("")||tf_zhongliang.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写田丰记录", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (jz_checi.getText().toString().equals("")||jz_zhongliang.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写进站粪便合计", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (gy_zhongliang.getText().toString().equals("")||gy_checi.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写固液分离残渣(五路居)记录", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (operator.getText().toString().equals("")) {
                        Toast.makeText(this, "请填写操作人", Toast.LENGTH_SHORT).show();
                        return;
                    }
//                    if (additon.getText().toString().equals("")) {
//                        Toast.makeText(this, "请填写备注", Toast.LENGTH_SHORT).show();
//                        return;
//                    }
                    String url = Config.url + "/sjq/weightProduction/addFormFaeces";
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String code = jsonObject.getString("code");

                                        if (code.startsWith("2")) {
                                            Toast.makeText(WeighingProductionSiteRecordForm_stool1_1.this,
                                                    "提交成功", Toast.LENGTH_SHORT).show();
                                        }
                                        else
                                            Toast.makeText(WeighingProductionSiteRecordForm_stool1_1.this,
                                                    "提交失败", Toast.LENGTH_SHORT).show();
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(WeighingProductionSiteRecordForm_stool1_1.this,
                                    "提交失败 " + error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() {
                            Map<String, String> map=new HashMap<>();
                            map.put("editTextDate", editTextDate.getText().toString());
                            map.put("editTextStartTime", editTextStartTime.getText().toString());
                            map.put("editTextCloseTime", editTextCloseTime.getText().toString());
                            map.put("editTextChengLiang", editTextChengLiang.getText().toString());
                            map.put("editTextJianKong", editTextJianKong.getText().toString());
                            map.put("editTextShuJu", editTextShuJu.getText().toString());
                            map.put("editTextYuanCheng", editTextYuanCheng.getText().toString());
                            map.put("hd_checi", hd_checi.getText().toString());
                            map.put("hd_zhongliang", hd_zhongliang.getText().toString());
                            map.put("sq_checi", sq_checi.getText().toString());
                            map.put("sq_zhongliang", sq_zhongliang.getText().toString());
                            map.put("bs_checi", bs_checi.getText().toString());
                            map.put("bs_zhongliang", bs_zhongliang.getText().toString());
                            map.put("tf_checi", tf_checi.getText().toString());
                            map.put("tf_zhongliang", tf_zhongliang.getText().toString());
                            map.put("jz_checi", jz_checi.getText().toString());
                            map.put("jz_zhongliang", jz_zhongliang.getText().toString());
                            map.put("gy_checi", gy_checi.getText().toString());
                            map.put("gy_zhongliang", gy_zhongliang.getText().toString());
                            map.put("operator", operator.getText().toString());
                            map.put("additon", additon.getText().toString());
                            map.put("factory", factory);
//                            Log.d("info", String.valueOf(map));
                            return map;

                        }

                    };
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                    queue.add(stringRequest);
                }).show();
    }


}